﻿$(document).ready(function () {
    // SetBodyWH();
    InitData()
});
//动态设置展示数据的div的宽高
function SetBodyWH() {
    var width = screen.availWidth;
    var heigth = screen.availWidth;
    var div = document.getElementsByClassName("maincontent");
    div.style.width = div + "px";
    div.style.heigth = heigth / 5 + "px";
}
var _id = "0";
var searchText = "";
// 初始化数据列表
function InitData() {
    _id = "0";
     searchText = "";
    $.post("Home/InitData", function (ret) {
        if (ret.Code == "0") {
            //获取数据对象
            var list = eval(ret.Data);
            var content = "";
            _id = list[list.length - 1].ProductId;
            SetContent(list, false);
            
        }
        else {
            GetNoDataNotice();
        }
    })

}
//设置加载更多 可见
function SetLoadMoreVisable() {
    $("#loadmore").css("display", "block");
}
//设置加载更多 可见
function SetLoadMoreNone() {
    $("#loadmore").css("display", "none");
}
//加载动画出现
function LoadingVis() {
    $("#loading").css("display", "block");
}
//加载动画消失
function LoadingNone() {
    $("#loading").css("display", "none");
}
//加载更多数据
function LoadMoreData() { 
    SetLoadMoreNone();
    LoadingVis(); 
    $.post("Home/LoadMoreData", { id: _id, searchKey: searchText }, function (ret) {
        if (ret.Code == "0") {
            //获取数据对象
            var list = eval(ret.Data);
            var content = "";
            _id = list[list.length - 1].ProductId;
           
            SetContent(list, true);
            LoadingNone();
        }
        else {
            LoadingNone();
            LastNewData();
        }
    })
}
//点击搜索按钮
function clickButton() { 
    var text = $("#searchfixfield").val();
    searchText = text;
    if (text != null && text != "") {
        SetLoadMoreNone();
        LoadingVis();
        $.post("Home/QueryDataByKeyName", { id: "0", searchKey: searchText }, function (ret) {
          
            if (ret.Code == "0") {
                //获取数据对象
                var list = eval(ret.Data);
                var content = "";
                _id = list[list.length - 1].ProductId;

                SetContent(list, false);
                LoadingNone();
            }
            else {

                LoadingNone();
                GetNoDataNotice();
            }
        })
    }
    CloseSearch();
}
///弹出搜索框
function Search() {
    $("#searchfixfield").val("");
    $("#searchdiv").css("display", "block");
}
//关闭搜索框
function CloseSearch() {
    $("#searchdiv").css("display", "none");
}
function GetNoDataNotice() {
    var nodata = '<div id="nodata" class="rankhead">' +
               '<div class="rankheadtitle">没有找到符合您查询词的结果。</div></div>'
    $("#listarea").html(nodata);
}
function LastNewData() {
    var nodata = '<div id="nodata" class="rankhead">' +
               '<div class="rankheadtitle">加载完所有数据。</div></div>'
    $("#listarea").append(nodata);
}
function SetContent(list, isAppend) {
    var content = "";
    if (list == null || list.length == 0) {
        if (!isAppend) {

            GetNoDataNotice();
        }
        return;
    }
    for (var index = 0; index < list.length; index++) {
        var tim = list[index].UpdateTimeStr.substring(5, list[index].UpdateTimeStr.length);
        //赋值
        content += '<div class="item"><div class="leftcontent">' +
            '<a class="title" href="' + list[index].Memo + '" target="_blank">' +
            '' + list[index].Pname + '</a>' +
            '<div class="clear"></div>'+
           
            ' <div class="clear"></div> <a class="thumbnail" href="' + list[index].Memo + '" target="_blank" >' +
            '<img src="'+list[index].ImgUrl + '" alt="' + list[index].Pname + '" style="width:100px;height:100px;margin-left:0px;margin-top:0px;margin-bottom:0px;margin-right:0px;">' +
            '</a> <div class="infoandabstract">' +
            '<div class="clear"></div>' +
            '<div class="mallname">' + list[index].Source + '</div><br/>' +functrn
            '<div style="margin-top: 5%;font-size:12px" ><span ">' + tim + '</span>从&nbsp;' + list[index].FromWeb + '&nbsp;同步</div>' +
            '<div class="clear"></div>' +
            '<div class="price" > ' + list[index].Price + '</div>' +
            '<div class="clear"></div>' +
            '<a class="buy" href="' + list[index].Memo + '" target="_blank""><span class="buyword">查看</span></a>' +
            ' </div> </div> </div>';

    } if (isAppend) {
        $("#listarea").append(content);

    }
    else {
        $("#listarea").html(content);
    }
    if (list.length == 30) {
        SetLoadMoreVisable();
    } else { LastNewData(); }
}
$('img').error(function () {
    $(this).attr('src', '/Images/none.png');
});
//onerror = "this.src=../Images/none.png"